package com.zhuifeng.product.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zhuifeng.common.log.annotation.Log;
import com.zhuifeng.common.log.enums.BusinessType;
import com.zhuifeng.common.security.annotation.RequiresPermissions;
import com.zhuifeng.product.domain.ProductRule;
import com.zhuifeng.product.service.IProductRuleService;
import com.zhuifeng.common.core.web.controller.BaseController;
import com.zhuifeng.common.core.web.domain.AjaxResult;
import com.zhuifeng.common.core.utils.poi.ExcelUtil;
import com.zhuifeng.common.core.web.page.TableDataInfo;

/**
 * 商品规格Controller
 * 
 * @author zhuifeng
 * @date 2022-08-18
 */
@RestController
@RequestMapping("/productRule")
public class ProductRuleController extends BaseController
{
    @Autowired
    private IProductRuleService productRuleService;

    /**
     * 查询商品规格列表
     */
    @RequiresPermissions("product:productRule:list")
    @GetMapping("/list")
    public TableDataInfo list(ProductRule productRule)
    {
        startPage();
        List<ProductRule> list = productRuleService.selectProductRuleList(productRule);
        return getDataTable(list);
    }

    /**
     * 查询类型规格
     *
     * @param typeId 类型主键
     * @return 类型规格
     */
    @RequiresPermissions("product:productRule:getRuleByTypeId")
    @GetMapping("/getRuleByTypeId/{typeId}")
    public AjaxResult getRuleByTypeId(@PathVariable("typeId") String typeId)
    {
        return AjaxResult.success(productRuleService.selectProductRuleByTypeId(typeId));
    }

    /**
     * 导出商品规格列表
     */
    @RequiresPermissions("product:productRule:export")
    @Log(title = "商品规格", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductRule productRule)
    {
        List<ProductRule> list = productRuleService.selectProductRuleList(productRule);
        ExcelUtil<ProductRule> util = new ExcelUtil<ProductRule>(ProductRule.class);
        util.exportExcel(response, list, "商品规格数据");
    }

    /**
     * 获取商品规格详细信息
     */
    @RequiresPermissions("product:productRule:query")
    @GetMapping(value = "/{productRuleId}")
    public AjaxResult getInfo(@PathVariable("productRuleId") String productRuleId)
    {
        return AjaxResult.success(productRuleService.selectProductRuleByProductRuleId(productRuleId));
    }

    /**
     * 新增商品规格
     */
    @RequiresPermissions("product:productRule:add")
    @Log(title = "商品规格", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductRule productRule)
    {
        return toAjax(productRuleService.insertProductRule(productRule));
    }

    /**
     * 修改商品规格
     */
    @RequiresPermissions("product:productRule:edit")
    @Log(title = "商品规格", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductRule productRule)
    {
        return toAjax(productRuleService.updateProductRule(productRule));
    }

    /**
     * 删除商品规格
     */
    @RequiresPermissions("product:productRule:remove")
    @Log(title = "商品规格", businessType = BusinessType.DELETE)
	@DeleteMapping("/{productRuleIds}")
    public AjaxResult remove(@PathVariable String[] productRuleIds)
    {
        return toAjax(productRuleService.deleteProductRuleByProductRuleIds(productRuleIds));
    }
}
